package com.pengtu.gsj.service.impl.check;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.CheckModuleDao;
import com.pengtu.gsj.entity.CheckResultVo;
import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.entity.check.CheckModule;
import com.pengtu.gsj.service.check.CheckModuleService;
import com.pengtu.gsj.service.system.SysmenuManagerService;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class CheckModuleServiceImpl implements CheckModuleService {
	
	@Autowired
	private CheckModuleDao checkModuleDao;
	@Autowired
	private SysmenuManagerService sysmenuManagerService;

	@Override
	public void save(CheckModule checkModule) {
		checkModuleDao.save(checkModule);
	}

	@Override
	public void getTotalCheckResult(String projectId) {
		if (StringUtils.isEmpty(projectId)) {
			return ;
		}
		
		//得到国省检所检查的模块
		List<Sysmenu> sysmenus = sysmenuManagerService.getCheckModule(projectId);
		CheckModule checkModule = null;
		for (Sysmenu sysmenu : sysmenus) {
			//根据projectId 与moduleId 查询记录是否存在  若存在 则比较结果是否更改， 若变化 则更新
			checkModule = getCheckModulesByproIdAndModId(projectId, sysmenu.getId());
			if (checkModule == null) {
				checkModule  = new CheckModule();
				checkModule.setProjectId(projectId);
				checkModule.setModuleId(sysmenu.getId());
				checkModule.setModuleName(sysmenu.getName());
				checkModule.setSort(sysmenu.getSort());
			}
		
			
			//查询每个模块所对应的检查得分
			List<CheckResultVo> checkResultVos = sysmenuManagerService.getCheckResultVoByParentId(projectId, sysmenu.getId());
			if (checkResultVos.size()>0) {
				CheckResultVo checkResultVo = checkResultVos.get(0);
				if (checkResultVo.getTableFull() == null) {
					checkResultVo.setTableFull(0.0);
				}
				if (checkResultVo.getTestFull() == null) {
					checkResultVo.setTestFull(0.0);
				}
				if (checkResultVo.getPerfection() == null) {
					checkResultVo.setPerfection(0.0);
				}
				checkModule.setTableFull(checkResultVo.getTableFull());
				checkModule.setTestFull(checkResultVo.getTestFull());
				checkModule.setPerfection(checkResultVo.getPerfection()*100);
			}
			save(checkModule);
		}
	}

	/**
	 * 
	 * getCheckModulesByproIdAndModId:
	 * 适用:根据projectId和moduleId查询记录
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public CheckModule getCheckModulesByproIdAndModId(String projectId, String moduleId) {
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("projectId", projectId, MatchType.EQ);
		queryFilter.addFilter("moduleId", moduleId, MatchType.EQ);
		List<CheckModule> list = checkModuleDao.find(queryFilter);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<CheckModule> getCheckModulesByProjectId(String projectId) {
		return checkModuleDao.getCheckModule(projectId);
	}

	@Override
	public List<Map<String, Object>> getCheckResultByProjectId(String projectId) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = null;
		List<CheckModule> checkModules = getCheckModulesByProjectId(projectId);
		double totalTableFull = 0.0;
		double totalTestFull = 0.0;
		for (CheckModule checkModule : checkModules) {
			map = new HashMap<String, Object>();
			map.put("name", checkModule.getModuleName());
			map.put("tableFull", checkModule.getTableFull());
			map.put("testFull", checkModule.getTestFull());
			map.put("perfection", MathsUtils.formatDouble(checkModule.getPerfection(), 2));
			map.put("id", checkModule.getModuleId());
			list.add(map);
			totalTableFull += checkModule.getTableFull();
			totalTestFull += checkModule.getTestFull();
		}
		map = new HashMap<String, Object>();
		map.put("name", "总体");
		map.put("tableFull", totalTableFull);
		map.put("testFull", totalTestFull);
		map.put("perfection", MathsUtils.divDouble(totalTestFull*100, totalTableFull, 2));
		list.add(map);
		return list;
	}
}
